package com.example.uploadtest.hsf;

import com.youku.yklive.hsf.Result;
import com.youku.yklive.hsf.identity.HsfIdentityInfoService;
import com.youku.yklive.hsf.identity.IdentityQuery;
import com.youku.yklive.hsf.identity.dto.IdentityInfoDTO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/announcement")
public class ExcelDownController {

    //    @DubboReference(
//            group = "HSF",
//            version = "1.0.0",
//            timeout = 10000, check = false)
    private HsfIdentityInfoService hsfIdentityInfoService;

    @GetMapping("/export")
    public void exportAnnouncements(HttpServletResponse response) throws IOException {
        log.info(".........进来了.........");
        // 1. 查询数据（示例）
        IdentityQuery query = new IdentityQuery();
        query.setOffset(0);
        query.setSize(100);
        Result<List<IdentityInfoDTO>> listResult = hsfIdentityInfoService.listIdentities(query);
        if (!listResult.isSuccess()) {
            log.error("查询数据失败：{}", listResult.getMsg());
            return;
        }
        List<IdentityInfoDTO> data = listResult.getData();
        log.info("data: {}", data);
        List<TestModel> dataList = new ArrayList<>();
        for (int i = 0; i < 100; i++) {
            TestModel testModel = new TestModel();
            testModel.setId(Long.valueOf(i));
            testModel.setTitle("公告标题" + i);
            testModel.setAnnounceTime("2023-05-01 12:00:00");
            dataList.add(testModel);
        }
        log.info("dataList: {}", dataList);
        // 2. 执行导出
        ExcelUtil.export(
                response,
                "公告列表_" + System.currentTimeMillis(),
                "公告数据",
                dataList,
                TestModel.class
        );
        log.info("导出成功");
    }
}
